﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace ShopOnline
{
    public partial class AdminMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            divAdmin.Visible = View_Users.GetCurrentUser() != null;
            lblAdmin.Text = View_Users.GetCurrentUser() != null ? "Hello " + View_Users.GetCurrentUser().s_FullName : "";
            if(!IsPostBack)
                LoadTags();
        }

        protected void LoadTags()
        {
            View_ProductTags tags = new View_ProductTags();
            tags.Query.AddOrderBy(View_ProductTags.ColumnNames.Count, MyGeneration.dOOdads.WhereParameter.Dir.DESC);
            tags.Query.Top = 10;
            tags.Query.Load();

            string html = "";
            int[] fontSize = new int[] { 145, 100, 75 };
            int j = 0;

            List<string> words = new List<string>();

            for (int i = 0; i < tags.RowCount; i++)
            {
                words.Add(string.Format("<li><a style='font-size:{1}%' href='Search.aspx?tag={0}'>{0}</a></li>", tags.s_Tag, fontSize[j]));
                if (i % fontSize.Length == 0 && j < fontSize.Length)
                    j++;

                tags.MoveNext();
            }
            Random rnd = new Random();
            while (words.Count > 0)
            {
                int num = rnd.Next(words.Count);
                html += words[num];
                words.RemoveAt(num);
            }

            litTags.Text = html;
        }

        public void SetPageTitle(string title)
        {
            lblPageTitle.Text = title;

            NavObject[] objs = new NavObject[2] { 
                new NavObject(){ Title="Control Panel", Url=title != "" ? this.ResolveUrl("~/ControlPanel.aspx") : "", ToolTip="Goto Control Panel"},
                new NavObject(){ Title=title, Url="", ToolTip=title}
            };

            ((GeneralMaster)this.Master).SetPageTitle(objs);
        }

        protected void btnLogout_OnClick(object sender, EventArgs e)
        {
            View_Users.SetCurrentUser(null);
            Response.Redirect("LoginAdmin.aspx");
        }
    }
}